from django.db import models

# Create your models here.

class Tag(models.Model):
    """
    资源标签
    """
    name = models.CharField(max_length=1024)

    class Meta:
        db_table = 'tag'

class Resource(models.Model):
    """
    资源
    """
    name = models.CharField(max_length=100, null=True, blank=True)
    link = models.CharField(max_length=100, null=True, blank=True)
    url = models.CharField(max_length=1024)
    method = models.CharField(max_length=100)
    tag = models.ManyToManyField(Tag)

    class Meta:
        db_table = 'resource'
        managed = True
        verbose_name = '资源'
        verbose_name_plural = '资源'


class Access(models.Model):
    """
    历史记录
    """
    url = models.CharField(max_length=1024, null=True, blank=True)
    method = models.CharField(max_length=100, null=True, blank=True)
    resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
    headers = models.CharField(max_length=1024, null=True, blank=True)
    cookies = models.CharField(max_length=2048, null=True, blank=True)
    params = models.CharField(max_length=2048, null=True, blank=True)
    data = models.CharField(max_length=2048, null=True, blank=True)
    resp_status = models.IntegerField(default=200)
    resp_content_type = models.CharField(max_length=1024, null=True, blank=True)
    resp_headers = models.CharField(max_length=1024, null=True, blank=True)
    resp_cookies = models.CharField(max_length=1024, null=True, blank=True)
    resp_data = models.TextField(null=True, blank=True)

    class Meta:
        abstract = True

class History(Access):
    """
    历史记录
    """
    access_time = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    # access or test
    run_by = models.CharField(max_length=200, default="access")

    class Meta:
        db_table = 'history'

class App(models.Model):
    name = models.CharField(max_length=200)

    class Meta:
        db_table = 'app'

class TestCaseSet(models.Model):
    """
    测试集合
    """
    app = models.ForeignKey(App, on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=1024)

    class Meta:
        db_table = 'test_case_set'

class TestCase(Access):
    """
    测试用例
    """
    app = models.ForeignKey(App, on_delete=models.SET_NULL, null=True, blank=True)
    class Meta:
        db_table = 'test_case'


class Request(models.Model):
    url = models.CharField(max_length=1024)
    method = models.CharField(max_length=100)
    headers = models.CharField(max_length=1024, null=True, blank=True)
    cookies = models.CharField(max_length=2048, null=True, blank=True)
    data = models.CharField(max_length=2048, null=True, blank=True)

    class Meta:
        db_table = 'request' 
        managed = True 
        verbose_name = '请求'
        verbose_name_plural = '请求'

class Session(models.Model):
    headers = models.CharField(max_length=1024, null=True, blank=True)
    cookies = models.CharField(max_length=2048, null=True, blank=True)
